
package array;
// 704 二分查找
class TowSearch{
    public int search(int[] nums, int target) {
        if (nums.length < 1) {
            return -1;
        }
        int leftIndex = 0;
        int rightIndex = nums.length - 1;

        while (leftIndex <= rightIndex) {
            int midIndex = (rightIndex - leftIndex) / 2 + leftIndex;

            if (nums[midIndex] == target) {
                return midIndex;
            }
            if (nums[midIndex] > target) {
              rightIndex = midIndex - 1;
            }
            if (nums[midIndex] < target) {
                leftIndex = midIndex + 1;
            }
        }
         return -1;
        }
    }
// -1  
// #-1
// 0 0 0

// -1 0  
//# 0
// 0 0 1
// 1 1 1

//# -1
// 0 0 1
// 0   1

// -1 0 1 :-1 :0 :1
// -1 0 1 2  :-1 :0 :1 :2